package com.qinggeng.project.api.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.http.HttpUtils;
import com.qinggeng.common.utils.spring.SpringUtils;
import com.qinggeng.framework.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
public class JsapiTicketUtils {


    public static final String APPID ="wxd326e434d9269d8a";

    public static final String SECRET="053fd5fae07daa38fbbf6138e94e000c";

    public static final String WEIXIN_API_HOST="https://api.weixin.qq.com";

    public static final String ACCESS_TOKEN_PATH="/cgi-bin/token";

    public static final String GET_TICKET_PATH="/cgi-bin/ticket/getticket";

    private static final Integer EXPIRATION_TIME= 1;

    private static  final  String ACCESS_TOKEN="accessToken";

    private  RedisCache redisCache = SpringUtils.getBean(RedisCache.class);


    /**
     * 获取access_token
     * @return
     */
    public static String getAccessToken(){

        Map<String, String> querys = Maps.newHashMap();
        querys.put("grant_type", "client_credential");
        querys.put("appid", APPID);
        querys.put("secret", SECRET);
        try {
            HttpResponse httpResponse = HttpUtils.doPost(WEIXIN_API_HOST, ACCESS_TOKEN_PATH, null, querys);
            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
            return jsonObject.getString("access_token");
        } catch (Exception e) {
            log.error("获取AccessToken异常",e.getMessage());
            throw new CustomException("获取AccessToken异常");
        }

    }

    /**
     * 获取调用微信JS接口的临时票据
     *
     * @param
     * @return
     * @throws Exception
     */
    public static String getJsApiTicket() {

        Map<String, String> querys = Maps.newHashMap();
        querys.put("access_token", getAccessToken());
        querys.put("type", "jsapi");
        try {
            HttpResponse httpResponse= HttpUtils.doGet(WEIXIN_API_HOST, GET_TICKET_PATH, null, querys);
            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
            String ticket=jsonObject.getString("ticket");
            if (StringUtils.isNotEmpty(ticket)){
                return ticket.replaceAll("\"", "");
            }
            return "";
        } catch (Exception e) {
            log.error("获取JsApiTicket异常",e.getMessage());
            throw new CustomException("获取JsApiTicket异常");
        }

    }


    public String flushJsApiTicket(){

        log.info("---------------调用刷新access_token---------------");
        String jsapiTicket =redisCache.getCacheObject(ACCESS_TOKEN);
        if (StringUtils.isNotEmpty(jsapiTicket)) {
            return jsapiTicket;
        }
        jsapiTicket = JsapiTicketUtils.getJsApiTicket();
        redisCache.setCacheObject(ACCESS_TOKEN,jsapiTicket,EXPIRATION_TIME, TimeUnit.HOURS);
        return jsapiTicket;
    }

}
